<template>
  <div class='container'>
    <el-card>
      <el-row>
        <el-col>
          <el-form label-width="100px">
            <el-form-item label="关键字" >
              <el-input size="small" style="width: 350px" type="text" v-model="input" placeholder="根据编号搜索"></el-input>
            </el-form-item>
          </el-form>
        </el-col>
          <el-row type="flex" justify="end">
            <el-button size="small">清除</el-button>
            <el-button size="small" type="primary">搜索</el-button>
          </el-row>
      </el-row>
      <el-row>
        <el-col>
          <el-alert class="el-icon-info" style="margin-bottom: 20px" type="info">数据一共 {{ page.totals }} 条</el-alert>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
             <el-table :data="tableData" style="width: 100%" >
             <el-table-column
                prop="id"
                label="编号"
                width="180">
               </el-table-column>
               <el-table-column
               prop="questionType"
               label="题型"
                width="180"
                :formatter="formatterData">
               </el-table-column>
              <el-table-column
              label="题目编号"
              >
              <template v-slot="{ row }">
                <el-button type="text" v-for="(val, index) in row.questionIDs" :key="index" @click="wqe(row)">{{val.number}}</el-button>
              </template>
              </el-table-column>
              <el-table-column
              prop="addTime"
              label="录入时间">
              <template v-slot="{ row }">
                {{ row.addTime | parseTimeByString }}
              </template>
              </el-table-column>
              <el-table-column
              prop="totalSeconds"
              label="答题时间(s)">
              </el-table-column>
              <el-table-column
              prop="accuracyRate"
              label="正确率(%)">
              </el-table-column>
              <el-table-column
              prop="userName"
              label="录入人">
              </el-table-column>
              <el-table-column
              prop="address"
              label="操作">
              <template v-slot="{ row }">
                <div class="delete">
                  <el-button class="el-icon-delete el-button--danger el-button--small is-plain is-circle" title="删除" @click="removeRandoms(row)"></el-button>
                </div>
              </template>
              </el-table-column>
             </el-table>
        </el-col>
      </el-row>
      <el-row type="flex" justify="end">
          <el-pagination
          background
          @size-change="handleSizeChange"
           @current-change="handleCurrentChange"
          :current-page="page.page"
          :page-sizes="[20, 30, 50, 100]"
          :page-size="page.sizes"
          layout="prev,pager,next,sizes,jumper"
          :total="page.totals">
    </el-pagination>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions'
export default {
  data () {
    return {
      input: '',
      tableData: [],
      loading: false,
      page: {
        page: 1,
        pagesize: 20,
        totals: 0
      }
    }
  },
  created () {
    this.randoms()
  },
  methods: {
    async randoms () {
      this.loading = true
      const { data } = await randoms(this.page)
      console.log(data)
      this.tableData = data.items
      this.page.totals = data.counts
      this.loading = false
    },
    handleSizeChange (newPage) {
      this.page.pagesize = newPage
      this.randoms()
    },
    handleCurrentChange (newPage) {
      this.page.page = newPage
      this.randoms()
    },
    async removeRandoms (id) {
      try {
        await this.$confirm('确定删除该项吗？')
        await removeRandoms(id)
        this.$message.success('删除成功')
        this.randoms()
      } catch (error) {
        console.log(error)
      }
    },
    formatterData (a, b, c) {
      if (c === '1') {
        return '单选'
      } else if (c === '2') {
        return '多选'
      } else {
        return '简答'
      }
    }
  }
}
</script>

<style scoped lang="scss">
  .container {
    margin: 10px 10px;
  }
  .delete {
    box-sizing: border-box;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: normal;
    word-break: break-all;
    line-height: 23px;
    padding-right: 10px;
  }
</style>
